home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Mark Pilgrim / Pentominoes 1.4.1 / source / Pentominoes ƒ / Pent code / pent meat.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-30  |  5.0 KB  |  298 lines  |  [TEXT/KAHL]

  1. /**********************************************************************\
  2.  
  3. File:        pent meat.c
  4.  
  5. Purpose:    This module handles the pattern recognition that underlies
  6.             the whole game, figuring out what piece you've highlighted.
  7.  
  8.  
  9. Pentominoes - a 2-D geometry board game
  10. Copyright (C) 1993 Mark Pilgrim
  11.  
  12. This program is free software; you can redistribute it and/or modify
  13. it under the terms of the GNU General Public License as published by
  14. the Free Software Foundation; either version 2 of the License, or
  15. (at your option) any later version.
  16.  
  17. This program is distributed in the hope that it will be useful,
  18. but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20. GNU General Public License for more details.
  21.  
  22. You should have received a copy of the GNU General Public License
  23. along with this program in a file named "GNU General Public License".
  24. If not, write to the Free Software Foundation, 675 Mass Ave,
  25. Cambridge, MA 02139, USA.
  26.  
  27. \**********************************************************************/
  28.  
  29. #include "pent meat.h"
  30. #include "pent.h"
  31. #include "msg sounds.h"
  32. #include "msg graphics.h"
  33. #include "msg dialogs.h"
  34.  
  35. int            tempPiece[5][5];
  36. int            maxi,maxj;
  37.  
  38. void RemovePiece(void)
  39. {
  40.     int            iter,utter;
  41.     
  42.     for (iter=0; iter<gNumRows; iter++)
  43.         for (utter=0; utter<gNumCols; utter++)
  44.             if (Board[iter][utter]==-2)
  45.                 Board[iter][utter]=-1;
  46.     PieceUsed[gCurrentColor]=FALSE;
  47.     for (iter=0; iter<gNumPlayed; iter++)
  48.     {
  49.         if (PiecesPlayed[iter]==gCurrentColor)
  50.         {
  51.             for (utter=iter; utter<gNumPlayed; utter++)
  52.                 PiecesPlayed[utter]=PiecesPlayed[utter+1];
  53.             iter=gNumPlayed;
  54.         }
  55.     }
  56.     gNumPlayed--;
  57.     gNumHilited=0;
  58.     gCurrentColor=-2;
  59.     UpdateBoard();
  60.     DoSound(sound_undo);
  61. }
  62.  
  63. void PlaceUnknownPiece(void)
  64. {
  65.     int            pieceNum;
  66.     
  67.     pieceNum=PlacePiece(-1);
  68.     if (pieceNum>=0)
  69.     {
  70.         DoSound(sound_placepiece);
  71.         PieceUsed[pieceNum]=TRUE;
  72.         PiecesPlayed[gNumPlayed]=pieceNum;
  73.         gNumPlayed++;
  74.         gNumHilited=0;
  75.         UpdateBoard();
  76.         if (gNumPlayed==12)
  77.             WinGame();
  78.     }
  79.     else if (pieceNum==-1)
  80.     {
  81.         DoSound(sound_error);
  82.         ParamText("\pThat is not one of the 12 Pentominoes pieces.","\p","\p","\p");
  83.         PositionDialog('ALRT', generalAlert);
  84.         StopAlert(generalAlert,0L);
  85.     }
  86.     else if (pieceNum==-2)
  87.     {
  88.         DoSound(sound_error);
  89.         ParamText("\pThat Pentominoes piece has already been used.","\p","\p","\p");
  90.         PositionDialog('ALRT', generalAlert);
  91.         StopAlert(generalAlert,0L);
  92.     }
  93. }
  94.  
  95. int PlacePiece(int pieceNum)
  96. {
  97.     int            i,j;
  98.     Boolean        found;
  99.     int            shapeTop,shapeLeft;
  100.     int            returnValue;
  101.     
  102.     i=0;
  103.     do
  104.     {
  105.         j=0;
  106.         do
  107.         {
  108.             found=(Board[i][j]==-2);
  109.             j++;
  110.         }
  111.         while ((!found) && (j<gNumCols));
  112.         i++;
  113.     }
  114.     while ((!found) && (i<gNumRows));
  115.     shapeTop=i-1;
  116.     j=0;
  117.     do
  118.     {
  119.         i=0;
  120.         do
  121.         {
  122.             found=(Board[i][j]==-2);
  123.             i++;
  124.         }
  125.         while ((!found) && (i<gNumRows));
  126.         j++;
  127.     }
  128.     while ((!found) && (j<gNumCols));
  129.     shapeLeft=j-1;
  130.         for (i=0; i<5; i++)
  131.         for (j=0; j<5; j++)
  132.             tempPiece[i][j]=0;
  133.         i=0;
  134.     do
  135.     {
  136.         j=0;
  137.         do
  138.         {
  139.             if (Board[i+shapeTop][j+shapeLeft]==-2)
  140.                 tempPiece[i][j]=1;
  141.             j++;
  142.         }
  143.         while ((j<5) && (j<gNumCols-shapeLeft));
  144.         i++;
  145.     }        
  146.     while ((i<5) && (i<gNumRows-shapeTop));
  147.         maxi=5;
  148.     while (BlankRow(maxi-1))
  149.         maxi--;
  150.     maxj=5;
  151.     while (BlankCol(maxj-1))
  152.         maxj--;
  153.         found=FALSE;
  154.     
  155.     if (pieceNum>=0)
  156.         found=MatchPiece(pieceNum);
  157.     else
  158.     {
  159.         pieceNum=0;
  160.         do
  161.         {
  162.             found=MatchPiece(pieceNum);
  163.             pieceNum++;
  164.         }
  165.         while ((pieceNum<12) && (!found));
  166.         if (found)
  167.             pieceNum--;
  168.     }
  169.     if (found)
  170.     {
  171.         if (!PieceUsed[pieceNum])
  172.         {
  173.             for (i=0; i<gNumRows; i++)
  174.                 for (j=0; j<gNumCols; j++)
  175.                     if (Board[i][j]==-2)
  176.                         Board[i][j]=pieceNum;
  177.  
  178.             returnValue=pieceNum;
  179.         }
  180.         else
  181.         {
  182.             returnValue=-2;
  183.         }
  184.     }
  185.     else
  186.     {
  187.         returnValue=-1;
  188.     }
  189.     
  190.     return returnValue;
  191. }
  192.  
  193. Boolean BlankRow(int i)
  194. {
  195.     int            j;
  196.     
  197.     j=0;
  198.     while ((!tempPiece[i][j]) && (j<5))
  199.         j++;
  200.     return (j==5);
  201. }
  202.  
  203. Boolean BlankCol(int j)
  204. {
  205.     int            i;
  206.     
  207.     i=0;
  208.     while ((!tempPiece[i][j]) && (i<5))
  209.         i++;
  210.     return (i==5);
  211. }
  212.  
  213. Boolean MatchPiece(int pieceNum)
  214. {
  215.     int            i;
  216.     Boolean        done;
  217.     
  218.     i=0;
  219.     do
  220.     {        
  221.         RotatePiece();
  222.         done=CheckPiece(pieceNum);
  223.         i++;
  224.     }
  225.     while ((!done) && (i<4));
  226.     if (!done)
  227.     {
  228.         FlipPiece();
  229.         i=0;
  230.         do
  231.         {
  232.             RotatePiece();
  233.             done=CheckPiece(pieceNum);
  234.             i++;
  235.         }
  236.         while ((!done) && (i<4));
  237.     }
  238.     return done;
  239. }
  240.  
  241. Boolean CheckPiece(int pieceNum)
  242. {
  243.     int            i,j;
  244.     Boolean        matched;
  245.     
  246.     i=0;
  247.     do
  248.     {
  249.         j=0;
  250.         do
  251.         {
  252.             matched=(tempPiece[i][j]==gShapes[pieceNum][i][j]);
  253.             j++;
  254.         }
  255.         while ((matched) && (j<5));
  256.         i++;
  257.     }
  258.     while ((matched) && (i<5));
  259.     return matched;
  260. }
  261.  
  262. void RotatePiece(void)
  263. {
  264.     int            otherPiece[5][5];
  265.     int            i,j;
  266.     int            temp;
  267.     
  268.     for (i=0; i<maxi; i++)
  269.         for (j=0; j<maxj; j++)
  270.             otherPiece[i][j]=tempPiece[i][j];
  271.  
  272.     for (i=0; i<5; i++)
  273.         for (j=0; j<5; j++)
  274.             tempPiece[i][j]=0;
  275.  
  276.     for (i=0; i<maxi; i++)
  277.         for (j=0; j<maxj; j++)
  278.             tempPiece[j][maxi-i-1]=otherPiece[i][j];
  279.             
  280.     temp=maxj;
  281.     maxj=maxi;
  282.     maxi=temp;
  283. }
  284.  
  285. void FlipPiece(void)
  286. {
  287.     int            i,j;
  288.     int            temp;
  289.         
  290.     for (i=0; i<maxi; i++)
  291.         for (j=0; j<maxj/2; j++)
  292.         {
  293.             temp=tempPiece[i][j];
  294.             tempPiece[i][j]=tempPiece[i][maxj-j-1];
  295.             tempPiece[i][maxj-j-1]=temp;
  296.         }
  297. }
  298.